import Vue from 'vue';
// 引入 axios
import axios from 'axios'
import qs from 'qs'

import {Toast} from 'vant';
Vue.use(Toast);

import {Dialog} from 'vant';
Vue.use(Dialog);

import {getToken} from "../utils/common";

let token = '';
getToken(data=>{
    token = data;
});


var domain = document.domain;
if(domain.includes("localhost")){
    axios.defaults.baseURL = process.env.baseURL;
}else{
    axios.defaults.baseURL = 'https://www.lingdaoyi.net:8393/stock-server';
}

//axios.defaults.headers.common['token'] = "71554e0f-2e46-44aa-8247-069493c21ed5";
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.timeout = 10000;


//请求前的钩子
axios.interceptors.request.use(function (config) {

    config.headers.authorization = token;

    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

//相应后的钩子
axios.interceptors.response.use(function (response) {

    //Toast.clear();
    if(response.data.code == "0"){
        return response.data;
    }else{
        return Promise.reject(response.data);
    }

}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});


var http = {

    /** get 请求
     * @param  {接口地址} url
     * @param  {请求参数} params
     */
    get: function (url, params) {
        return new Promise((resolve, reject) => {
            axios.get(url, {
                params: params
            })
                .then((response) => {
                    resolve(response.data);
                })
                .catch((error) => {
                    reject(error)
                    Toast.fail("请稍后再试...");
                });
        })
    },
    /** post 请求
     * @param  {接口地址} url
     * @param  {请求参数} params
     */
    post: function (url, params) {
        return new Promise((resolve, reject) => {
            axios.post(url, params)
                .then((response) => {
                    resolve(response.data);
                })
                .catch((error) => {
                    reject(error)
                    Toast.fail("网络错误");
                });
        })
    },
    /**
     *  put请求
     * @param  {接口地址} url
     * @param  {请求参数} params
     * @returns {Promise<any>}
     */
    put: function (url, params) {
        return new Promise((resolve, reject) => {
            axios.put(url, qs.stringify(params))
                .then((response) => {
                    resolve(response.data);
                })
                .catch((error) => {
                    reject(error);
                });
        })
    },
    /**
     *  delete请求
     * @param  {接口地址} url
     * @param  {请求参数} params
     * @returns {Promise<any>}
     */
    delete: function (url, params) {
        return new Promise((resolve, reject) => {
            axios.delete(url, {
                params: params
            })
                .then((response) => {
                    resolve(response.data);
                })
                .catch((error) => {
                    reject(error);
                });
        })
    },
    upload: function (url, params) {
        return new Promise((resolve, reject) => {
            let config = {
                headers: {
                    "Content-Type": "multipart/form-data"
                }
            };
            axios.post(url, params, config).then((response) => {
                resolve(response.data);
            }).catch((error) => {
                Toast.fail("网络错误");
            })
        })
    }
};
export default http

